/** @author: duanhaijiao  @date 2022/7/8--17:30 **/


import { home } from '@/assets/api/api.js'
export const pageMixin = {
    props:{
        pageId:''
    },
    data(){
        return {
            showDetail:false,
            dataList:[],
            dataTotal:null,
            detailInfo:{},
        }
    },
    watch:{
        pageId:function(value){
            if(localStorage.getItem('toShowPageInfo')){
                this.showDetail = true;
                this.getDetail(this.$store.state.detailId);
                localStorage.removeItem('toShowPageInfo')
            }else{
                this.showDetail = false;
                this.getListData()
            }
        },
        // "$store.state.detailId":function() {
        //     if(this.$store.state.detailId == '-1'){
        //         this.showDetail = false;
        //         this.getListData();
        //     }else {
        //         this.showDetail = true;
        //         this.getDetail(this.$store.state.detailId)
        //     }
        // },

    },

    mounted() {
        if(localStorage.getItem('toShowPageInfo')){
            this.showDetail = true;
            this.getDetail(this.$store.state.detailId);
            localStorage.removeItem('toShowPageInfo')
        }else{
            this.showDetail = false;
            this.getListData()
        }

    },
    methods:{
        getListData(){
            if(!this.pageId || this.showDetail) return;
            this.$http(home.articlelist,{
                page:this.pageNum,
                size:this.pageSize,
                cate_id:this.pageId
            }).then((res)=>{
                this.showDetail = false;
                this.dataList = res.data.data;
                this.dataTotal = res.data.total
            })
        },
        getDetail(data){
            if(data.jumptype == 'default' && data.url){
                window.location.href = data.url
            }else{
                this.$store.state.detailId = data.id||data;
                this.showDetail = true;
                this.$http(home.articledeail,{
                    id:data.id||data
                }).then((res)=>{
                    this.detailInfo = res.data
                })
            }

        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.pageNum = val;
            this.getListData()
        },
        sliderSelect(data){
            console.log(data)
        }
    }
}